<?php


namespace app\admin\controller;

use app\api\service\GPService;
use app\common\controller\AdminBaseController;
use app\common\model\tang\Invoice as InvoiceModel;
use app\common\model\tang\InvoiceLog;
use app\common\model\tang\MonthElectricityDegree;
use app\common\model\tang\MonthElectricityRecord;
use think\facade\Db;
use app\common\model\tang\Merchant;
use app\common\model\tang\Cost;
use think\facade\Log;

class Invoice extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new InvoiceModel();
    }

    //电费账单列表
    public function index()
    {
        if (IS_AJAX) {
            return $this->model->getAdminPageData($this->param, '', []);
        }
        $merchant_list =getMerchant();
        //获取菜单列表
        $this->assign(compact("merchant_list"));
        return $this->fetch();
    }

    /**
     * 查看订单详情
     */
    public function detail()
    {
        $param = $this->param;
        $list = (new \app\common\model\tang\Invoice())->with(['merchant'])->find($param['id']);
        $ids = explode(',',$list['costId']);
        $costInfo= (new Cost())->with(['costType'])->whereIn('id',$ids)->field('id,order_sn,cost_type_id,all_amount,start_time,end_time')->select()->toArray();
        foreach ($costInfo as $kk=>$vv){
            $costInfo[$kk]['start_time'] = date('Y-m-d',$vv['start_time']);
            $costInfo[$kk]['end_time'] = date('Y-m-d',$vv['end_time']);
        }
        $list['amount'] = (new Cost())->whereIn('id',$ids)->sum('all_amount');
        $list['costInfo'] = $costInfo;
        $this->assign('list', $list);
        return $this->fetch();
//        $where[] = ['I.id', '=', $param['id']];
//        $list = $this->model->alias('I')
//            ->leftjoin('tang_cost c', 'I.costId=c.id')
//            ->leftjoin(['tang_merchant' => 'm'], "c.merchant_id = m.id")
//            ->where($where)
//            ->field('I.*,m.name,m.company,c.amount,c.real_amount,c.service_charge,c.electrity_type')->find()->toArray();
//        $this->assign('list', $list);
//        return $this->fetch();
    }

    /**
     *开票驳回
     */
    public function refuse(){
        $param = $this->param;
        $info = $this->model->find($param['id']);
        if(empty($info)){
            return self::ajaxResult([], 0, 0, '开票信息不存在');
        }
        $result = (new Cost())->whereIn('id',$info['costId'])->update(['invoice'=>0]);
        $this->model->update(['status'=>2],['id'=>$param['id']]);
        if($result){
            return self::ajaxResult([], 0, 1,'驳回成功');
        }
        return self::ajaxResult([], 0, 0, '驳回失败');
    }
    /**
     *开票成功
     */
    public function ticket(){
        $param = $this->param;
        $info = $this->model->find($param['id']);
        if(empty($info)){
            return self::ajaxResult([], 0, 0, '开票信息不存在');
        }
        $result = (new Cost())->whereIn('id',$info['costId'])->update(['invoice'=>1]);
        $this->model->update(['status'=>1],['id'=>$param['id']]);
        if($result){
            return self::ajaxResult([], 0, 1,'开票成功');
        }
        return self::ajaxResult([], 0, 0, '开票失败');
    }
    /**
     * 冲红发票
     */
    public function blush(){
        $param = $this->param;
        $info = $this->model->find($param['ids']);
        if(empty($info)){
            return self::ajaxResult([], 0, 0, '开票信息不存在');
        }
        if($info['status']!=1){
            return self::ajaxResult([], 0, 0, '此发票未开具或已冲红');
        }
        $GPservice = new GPService();
        $FPQQLSH = 'HFZD' . date('YmdHi') . rand(1000, 9999);
        $contentData = [
            'REQUEST_COMMON_FPKSHC'=>[
                'FPQQLSH'=>$FPQQLSH,
                'XSF_NSRSBH'=>'12345678900001BWDZ',
                'XSF_MC'=>'中国联合网络通信集团有限公司',
                'YFP_DM'=>$info['invoiceCode'],
                'YFP_HM'=>$info['invoiceNo'],
                'CHYYDM'=>'4',
            ]
        ];

        $this->model->update([
            'invoiceBlush'=>$FPQQLSH
        ],[
            'id'=>$param['id']
        ]);
        $result = $GPservice->blush($contentData);
        return self::ajaxResult([], 0, 1, $result['returnMessage']);
    }
    /**
     * 查看失败原因
     */
    public function failreason(){
        $param = $this->param;
        $info = $this->model->find($param['id']);
        if(empty($info)){
            return self::ajaxResult([], 0, 0, '开票信息不存在');
        }
        $invoiceSerialNum = $info['invoiceSerialNum'];
        $involceLog = new InvoiceLog();
        $result = $involceLog->where('send_info','like',"%".$invoiceSerialNum."%")->find();
        if (is_numeric($result['return_info'])){
            $info =$result['return_info'];
        }else{
            $info = json_decode($result['return_info'],true);
        }
        $this->assign('info',$info);
        return $this->fetch();
    }
}